package hang.xipujewelWeb.web.account;

import hang.xipujewelWeb.entity.account.User;
import hang.xipujewelWeb.entity.app.ExchangeRate;
import hang.xipujewelWeb.entity.app.Jewel;
import hang.xipujewelWeb.entity.app.Order;
import hang.xipujewelWeb.entity.app.Ring;
import hang.xipujewelWeb.entity.app.ShopAddress;
import hang.xipujewelWeb.pojo.authority.WebViews;
import hang.xipujewelWeb.service.account.ExchangeRateManager;
import hang.xipujewelWeb.service.account.OrderManager;
import hang.xipujewelWeb.service.account.UserManager;
import hang.xipujewelWeb.utils.UserUtil;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import net.esoar.modules.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户管理
 * 
 * @author qizai
 * 
 */
@Controller
@RequestMapping(WebViews.SYSTEM_BASE_USER)
public class UserController extends BaseController<User> {
	// 权限及路径
	private final static String VIEW = WebViews.R_USER_VIEW;
	private final static String EDIT = WebViews.R_USER_EDIT;
	private final static String MODEL = WebViews.MODEL_USER;
	private final static String BASE_MAPPING = WebViews.SYSTEM_BASE_USER;

	@Autowired
	private UserManager userManager;

	@Autowired
	private ExchangeRateManager exchangeRateManager;

	@Autowired
	private OrderManager orderManager;

	@RequestMapping("")
	public String myadmin(Model model, ServletRequest request) {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		//		List<Order> orders = cUser.getOrders();
		List<Order> orders = orderManager.findAllByUser(cUser.getId());
		ShopAddress shopAddress = cUser.getShopAddress();
		initSumPrice(orders);
		model.addAttribute("orders", orders);
		model.addAttribute("shopAddress", shopAddress);
		return "account/myadmin";
	}

	@RequestMapping("updatePassword")
	@ResponseBody
	public String updatePassword(@RequestParam("oldPassword") String oldPassword,
			@RequestParam("newPassword") String newPassword) {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		if (!oldPassword.equals(cUser.getSourcePassword())) {
			return "旧密码不正确";
		} else {
			cUser.setPassword(newPassword);
			cUser.setSourcePassword(newPassword);
			userManager.updatePassword(cUser);
			return "修改密码成功";
		}
	}

	@RequestMapping("searchOrder")
	public String searchOrder(Model model, ServletRequest request) {
		Map<String, Object> searchParams = getFilters(request);
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		searchParams.put("EQ_user.id", cUser.getId());
		List<Order> orders = orderManager.search(searchParams);
		initSumPrice(orders);
		model.addAttribute("orders", orders);
		return "order/searchOrder";
	}

	public void initSumPrice(List<Order> orders) {
		ExchangeRate exchangeRate = exchangeRateManager.getAll().get(0);
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		for (Order order : orders) {
			float sum = 0;
			List<Jewel> jewels = order.getJewels();
			List<Ring> rings = order.getRings();
			if (jewels != null && jewels.size() > 0) {
				for (Jewel jewel : jewels) {
					float discount = (float) ((100.0 + jewel.getDiscount() + cUser.getParent().getPriceBaseNumber()
							.getJewelBaseNumBer()) / 100.0);
					float priceRMB = jewel.getWeight() * jewel.getInternationalPrice() * (discount)
							* exchangeRate.getRate();
					sum = sum + priceRMB;
				}
			}
			if (rings != null && rings.size() > 0) {
				for (Ring ring : rings) {
					if (cUser.getType() == 2) {
						float secondPrice = ring.getPrice()
								* cUser.getParent().getPriceBaseNumber().getRingBaseNumber();
						ring.setSecondPrice(secondPrice);
						sum = sum + ring.getSecondPrice();
					} else {
						sum = sum + ring.getPrice();
					}
				}
			}
			order.setSumPrice(sum);
		}
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute(MODEL)
	public User getModel(@RequestParam(value = ID, required = false) Long id) {
		if (id != null) {
			return userManager.get(id);
		}
		return null;
	}

	/**
	 * 不自动绑定对象中的allRoles属性，另行处理。
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.setDisallowedFields("allRoles");
	}

	//
	@Override
	public String baseMapping() {
		return BASE_MAPPING;
	}

	@Override
	public String getModelName() {
		return MODEL;
	}

}
